package com.carnation.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.carnation.bean.GroupMenu;
import com.carnation.bean.HeadMenu;
import com.carnation.bean.Menu;
import com.carnation.utils.Json;
import com.carnation.utils.MenuRead;
import com.carnation.utils.MenuWrite;
import com.carnation.utils.SessionInfo;
import com.google.gson.Gson;

public class AddMenuServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(AddMenuServlet.class);
	
	protected void doPost(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String type = httpServletRequest.getParameter("type");
		MenuRead menuRead = new MenuRead();
		MenuWrite menuWrite = new MenuWrite();
		Json json = new Json();
		String msg =null;
		String status=null;
		SessionInfo sessionInfo = null;
		if(httpServletRequest.getSession().getAttribute("sessionInfo") == null){
			status="500";
			msg = "登陆超时,请重新登陆";
			json.setStatus(status);
			json.setMsg(msg);
			Gson gson = new Gson();
			try{
				httpServletResponse.setCharacterEncoding("UTF-8");
				httpServletResponse.getWriter().write(gson.toJson(json));
				httpServletResponse.getWriter().close();
			}
			catch(Exception e){
				logger.error("响应书写错误",e);
			}
			return ;
		}
		sessionInfo = (SessionInfo)httpServletRequest.getSession().getAttribute("sessionInfo");
		if(type == null || type.isEmpty() ||!"headgroupmenu".contains(type)){
			status ="400";
			msg = "未知类型";
		}
		else if(type.equals("head")){
			String name = httpServletRequest.getParameter("name");
			String title = httpServletRequest.getParameter("title");
			String icon = httpServletRequest.getParameter("icon");
			if(menuRead.checkHeadNameExists(name)){
				msg = "头部菜单已存在";
				status="300";
			}
			else{
				String id =null;
				int i=5;
				do{
					id= Long.toString(System.currentTimeMillis());
					if(menuRead.getHeadMenuById(id) == null){
						break;
					}
					id = null;
				}while(i>0);
				if(id == null){
					status = "500";
					msg = "头部菜单ID初始化失败";
				}
				else{
					HeadMenu headMenu = new HeadMenu(id, name, title, name,icon);
					menuWrite.writeHead(headMenu);
					status="200";
					msg = "创建头部菜单成功" ;
					json.setObj(headMenu);
				}
			}
		}
		else if(type.equals("group")){
			String parentId = httpServletRequest.getParameter("parentId");
			String name = httpServletRequest.getParameter("name");
			String icon = httpServletRequest.getParameter("icon");
			if(menuRead.checkGroupNameExists(parentId, name)){
				msg = "组菜单已存在";
				status  = "300";
			}
			else{
				String id = null;
				int i = 5;
				do{
					id = Long.toString(System.currentTimeMillis());
					if(menuRead.getGroupMenuById(id) == null){
						break;
					}
					id = null;
				}while(i>0);
				if(id == null){
					status="500";
					msg="头部菜单ID初始化失败";
				}
				else {
					GroupMenu groupMenu = new GroupMenu(id, name, icon);
					menuWrite.writeGroupMenu(parentId, groupMenu);
					status ="200";
					msg="创建组菜单成功";
					json.setObj(groupMenu);
				}
			}
		}
		else if(type.equals("menu")){
			String parentId = httpServletRequest.getParameter("parentId");
			String name = httpServletRequest.getParameter("name");
			String icon = httpServletRequest.getParameter("icon");
			if(menuRead.checkMenueNameExists(parentId, name)){
				msg = "菜单已存在";
				status  = "300";
			}
			else{
				String id = null;
				int i = 5;
				do{
					id = Long.toString(System.currentTimeMillis());
					if(menuRead.getMenuById(id) == null){
						break;
					}
					id = null;
				}while(i>0);
				if(id == null){
					status="500";
					msg="头部菜单ID初始化失败";
				}
				else{
					String url = "/downLoad?id=" + id;
					Menu menu = new Menu(id, name, icon, url);
					menuWrite.writeMenu(parentId, menu,sessionInfo.getUser());
					status ="200";
					msg="创建组菜单成功";
					json.setObj(menu);
				}
			}
		}
		else{
			status ="unkown";
			msg="未知错误";
		}
		httpServletResponse.setContentType("text/html;charset=utf-8");
		try{
			json.setStatus(status);
			json.setMsg(msg);
			Gson gson = new Gson();
			httpServletResponse.getWriter().write(gson.toJson(json));
			httpServletResponse.getWriter().close();
		}
		catch(Exception e){
			logger.error("错误",e);
		}
	}
	
	protected void doGet(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		doPost(httpServletRequest, httpServletResponse);
	}
}
